#include <stdio.h>
#include <errno.h>

#define ZERO_NUM 0.000001

double power_unsigned_0(double base, unsigned int exp)
{
	unsigned int i;
	double result = 1.0;

	for (i = 1; i <= exp; i++) {
		result *= base;
	}
	
	return result;
}

double power_unsigned_1(double base, unsigned int exp)
{
	double result;

	if (exp == 0)
		return 1.0;
	if (exp == 1)
		return base;
	
	result = power_unsigned_1(base, exp >> 1);
	result *= result;
	if ((exp & 0x1) == 1)
		result *= base;
	
	return result;
}

int equal(double a, double b)
{
	if (a - b > -ZERO_NUM
		&& a -b < ZERO_NUM)
		return 1;
	else
		return 0;
}

double power(double base, int exp)
{
	double result;
	unsigned int exp_abs;

	if (equal(base, 0.0)) {
		if (exp < 0) {
			errno = -1;
			return 0;
		}
		return 0.0;
	}

	exp_abs = (unsigned int)(exp);
	if (exp < 0)
		exp_abs = (unsigned int)(-exp);
	
//	result = power_unsigned_0(base, exp_abs);
	result = power_unsigned_0(base, exp_abs);

	if (exp < 0)
		return (1.0 / result);
	return result;
}

void print_power(double base, int exp)
{
	printf("base:%f:exp:%d:::result:%f\n", base, exp, power(base, exp));
}

int main()
{

	print_power(2, 3);
	print_power(2, -3);
	print_power(0, 0);
	print_power(0, -3);

	return 0;
}
